package com.redpack.redpost.struts.action.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.redpack.redpost.struts.form.admin.contenidoForm;
import com.redpack.redpost.util.dao.adminDAO;
import com.redpack.redpost.util.to.generalUtil;

public class agregarcontenidoval extends Action {
	
	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
		boolean logginVal = generalUtil.isLoggedInAdmin(request);
		if(logginVal!=true)
			return mapping.findForward("noLogin") ;
		
		contenidoForm f = (contenidoForm)form;
		boolean val = false;
		if(f != null){
			if( f.getCve_contenido() > 0 ){
				val = adminDAO.updContenido(f.getCve_contenido(),f.getNombre(),f.getContenido(),f.getSubseccion(),f.getOrden());
				return mapping.findForward("successEdit") ;
			}else{
				val = adminDAO.saveContenido(f.getNombre(),f.getContenido(),f.getSubseccion(),f.getOrden());
			}
		}
		List subsecciones = new ArrayList();
		try {
			subsecciones = adminDAO.getSubsecciones();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("subsecciones", subsecciones);
		request.setAttribute("orden_sel", "0");
		
		if( val == true ){
			request.setAttribute("correcto", ""+val);
		}else{
			request.setAttribute("correcto", "");
		}
		
		return mapping.findForward("success") ;
	}

}
